package com.darren.befriend.adapters;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.darren.befriend.BaseActivity;
import com.darren.befriend.R;
import com.darren.befriend.utilities.Utilities;
import com.darren.befriend.views.CircleImageView;
import com.darren.befriend.views.HelveticaLTStdLight;
import com.darren.befriend.views.OnSwipeTouchListener;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class FriendsResultAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<ParseUser> mFriendtArr;
	private ParseUser mFriend;
	private ParseUser currentUser;
	private int[] hobbiesMatch = null;
	private boolean[] isShowLeft = null;
	private boolean[] isShowRight = null;
	private OnFriendItemClickListener mFriendListener;

	public FriendsResultAdapter(Context c, ArrayList<ParseUser> interestArr) {
		mContext = c;
		mFriendtArr = interestArr;
		hobbiesMatch = new int[] { R.drawable.icon_match_0,
				R.drawable.icon_match_1, R.drawable.icon_match_2,
				R.drawable.icon_match_3, R.drawable.icon_match_4,
				R.drawable.icon_match_5, R.drawable.icon_match_6,
				R.drawable.icon_match_7, R.drawable.icon_match_8 };
		if (mFriendtArr != null) {

			isShowLeft = new boolean[mFriendtArr.size()];
			isShowRight = new boolean[mFriendtArr.size()];
			for (int i = 0; i < isShowLeft.length; i++) {

				isShowLeft[i] = false;
				isShowRight[i] = false;
			}
		}
	}

	public void setOnFriendClickListener(OnFriendItemClickListener listener) {

		mFriendListener = listener;
	}

	@Override
	public int getCount() {

		if (mFriendtArr != null) {

			return mFriendtArr.size();
		}

		return 0;
	}

	@Override
	public Object getItem(int arg0) {

		return mFriendtArr.get(arg0);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public int getItemViewType(int position) {

		return 0;
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("unchecked")
	@Override
	public View getView(final int position, final View convertView,
			ViewGroup parent) {

		View item = null;
		InterestHolder holder = new InterestHolder();
		mFriend = mFriendtArr.get(position);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater
					.inflate(R.layout.layout_friends_item, parent, false);
			final InterestHolder holderTmp = holder;
			holder.matchContainer = (LinearLayout) item
					.findViewById(R.id.hobbies_match_container);
			holder.friendName = (HelveticaLTStdLight) item
					.findViewById(R.id.friend_name);
			holder.friendDistance = (HelveticaLTStdLight) item
					.findViewById(R.id.friend_address);
			holder.profileImg = (CircleImageView) item
					.findViewById(R.id.friend_icon);
			holder.likeBack = (ImageView) item.findViewById(R.id.like_back);
			holder.dislikeBack = (ImageView) item
					.findViewById(R.id.dislike_back);
			item.setOnTouchListener(new OnSwipeTouchListener(mContext) {
				@Override
				public void onSwipeLeft() {
					super.onSwipeLeft();

					if (holderTmp.likeBack.getVisibility() == View.GONE) {
						isShowLeft[position] = !isShowLeft[position];
						if (isShowLeft[position]) {

							holderTmp.likeBack.setVisibility(View.VISIBLE);
						} else {

							holderTmp.likeBack.setVisibility(View.GONE);
						}
					}
				}

				@Override
				public void onSwipeRight() {
					super.onSwipeRight();

					if (holderTmp.dislikeBack.getVisibility() == View.GONE) {
						isShowRight[position] = !isShowRight[position];
						if (isShowRight[position]) {

							holderTmp.dislikeBack.setVisibility(View.VISIBLE);
						} else {

							holderTmp.dislikeBack.setVisibility(View.GONE);
						}
					}
				}

				@Override
				public void onClick() {
					super.onClick();

					if (mFriendListener != null) {

						mFriendListener.onClick(position,
								mFriendtArr.get(position));
					}
				}
			});

			holder.dislikeBack.setOnTouchListener(new OnSwipeTouchListener(
					mContext) {
				@Override
				public void onSwipeLeft() {
					super.onSwipeLeft();
					if (holderTmp.dislikeBack.getVisibility() == View.VISIBLE) {

						holderTmp.dislikeBack.setVisibility(View.GONE);
					}
				}

				@Override
				public void onSwipeRight() {
					super.onSwipeRight();
				}

				@Override
				public void onClick() {
					super.onClick();
					if (mFriendListener != null) {

						mFriendListener.ondislikeBack(position,
								mFriendtArr.get(position));
					}
				}
			});

			holder.likeBack.setOnTouchListener(new OnSwipeTouchListener(
					mContext) {
				@Override
				public void onSwipeLeft() {
					super.onSwipeLeft();
				}

				@Override
				public void onSwipeRight() {
					super.onSwipeRight();
					if (holderTmp.likeBack.getVisibility() == View.VISIBLE) {

						holderTmp.likeBack.setVisibility(View.GONE);
					}
				}

				@Override
				public void onClick() {
					super.onClick();
					if (mFriendListener != null) {

						mFriendListener.onLikeBack(position,
								mFriendtArr.get(position));
					}
				}
			});

			Utilities.setFontType(mContext, holder.friendName);
			item.setTag(holder);
		} else {

			item = (View) convertView;
			holder = (InterestHolder) item.getTag();
		}

		if (mContext instanceof BaseActivity) {

			currentUser = ((BaseActivity) mContext).getCurrentUser();
		}

		// profile img
		String currentFullName = mFriend.get("fullName").toString();
		if (!currentFullName.contains("_local")) {

			String url = String
					.format(mContext.getResources().getString(
							R.string.profile_normal_url), mFriend.getUsername());
			((BaseActivity) mContext).getImageLoader().displayImage(url,
					holder.profileImg, ((BaseActivity) mContext).getOptions());
		} else {

			try {

				byte[] image = (byte[]) mFriend.get("Photo");
				Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0,
						image.length);
				holder.profileImg.setImageBitmap(bitmap);
			} catch (Exception e) {

				holder.profileImg.setImageResource(R.drawable.tmp_full_pic);

			}
		}

		// name
		ParseObject meInfo = (ParseObject) mFriend.get("MeInfo");
		String fullName = (String) mFriend.get("fullName");
		holder.friendName.setText(fullName.replace("_local", "") + ","
				+ (Integer) meInfo.get("MyAge"));

		// distance
		ParseGeoPoint pos = (ParseGeoPoint) mFriend.get("Position");
		ParseGeoPoint pos1 = null;
		pos1 = (ParseGeoPoint) currentUser.get("Position");
		if (pos != null && pos1 != null) {

			holder.friendDistance.setText(String.format(
					mContext.getString(R.string.away),
					(int) pos1.distanceInMilesTo(pos)));
		}

		// hobbies
		List<Boolean> hobby1 = (List<Boolean>) meInfo.get("Hobby");
		ParseObject myMeInfo = (ParseObject) currentUser.get("MeInfo");
		List<Boolean> hobby2 = (List<Boolean>) myMeInfo.get("Hobby");
		ArrayList<Integer> comms = new ArrayList<Integer>();
		for (int i = 0; i < 9; i++) {

			if (comms.size() >= 4) {

				break;
			}
			if (hobby1.get(i) && hobby2.get(i)) {

				comms.add(i);
			}
		}

		holder.matchContainer.removeAllViews();
		for (int i = 0; i < comms.size(); i++) {

			ImageView matchImg = new ImageView(mContext);
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			matchImg.setLayoutParams(params);
			matchImg.setImageResource(hobbiesMatch[comms.get(i)]);
			holder.matchContainer.addView(matchImg);
		}

		return item;
	}

	public interface OnFriendItemClickListener {

		public void onClick(int position, ParseUser user);

		public void onLikeBack(int position, ParseUser user);

		public void ondislikeBack(int position, ParseUser user);
	}

	static class InterestHolder {

		ImageView profileImg, likeBack, dislikeBack;
		TextView friendDistance, friendName;
		LinearLayout matchContainer;
	}
}
